<?php

class CustomersController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /customers
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        return View::make('customers.index',compact('customer'));
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /customers/create
	 *
	 * @return Response
	 */
    public function create()
    {
        //
        return View::make('customers.create');
    }
    public function reg()
    {
        //
        $type = Input::get('type');
        if($type == 'invite'){
            $customer_id = Input::get('customer_id');
            $customer = Customer::find($customer_id);
        }else{
            $type = 0;
            $customer = 0;
        }

        $areas = Area::where('level',1)->get();

        return View::make('customers.reg',compact('areas','type','customer'));
    }


    public function login(){


//        Session::put('customer_id', 1);
//        return Redirect::route('mounts.first');
//
//
        $invite = Input::get('invite');
        if(trim($invite) != ''){//已填写邀请码
            $invite_count = Customer::where('invite',$invite)->count();
            if($invite_count < 1){
                return Redirect::back()->withInput()->withErrors('请填写您的 email 地址');
            }else{
                $invite_customer = Customer::where('invite',$invite)->first();
                $invite_customer_id = $invite_customer->id;
            }
        }else{
            $invite_customer_id = null;
        }

        $value = Session::get('user_info');
        $customer          = new Customer;
        $customer->name   = Input::get('name');
        $customer->mobile   = Input::get('mobile');
        $customer->type   = 0;
        $customer->invite_id   = $invite_customer_id;
        $customer->all_points   = 0;
        $customer->use_points   = 0;
        $customer->city   = Input::get('c_id');
        $customer->province   = Input::get('p_id');
        $customer->area   = $invite_customer->area;//区域客户为邀请人相同区域
        $customer->sex   = Input::get('sex');
        $customer->openid   = $value['openid'];
        $customer->avatar   = $value['headimgurl'];
        $customer->invite = 0;
        $customer->save();


        $get_customer = Customer::find($customer->id);
        $get_customer->invite = sprintf('%06s', $customer->id);
        Customer::add_points($customer->id,'supplement',1,0);//增加积分
        $get_customer->supplement = 1;

        $get_customer->save();

        if(trim($invite) != '') {//已填写邀请码
            Customer::add_points($invite_customer_id,'inviter_reg',1,0,$customer->id);//增加积分
            Mountrecord::new_record(0, 'reg', $customer->id, $invite_customer_id);//添加邀请注册记录

            /**
             * 分销商粉丝数加1
             */
            if($invite_customer->area > 0){
                $user = User::where('permissions','{"section":1}')->where('area',$invite_customer->area)->first();
                $user->subscribe = $user->subscribe+1;
                $user->save();
            }


        }

        Session::put('customer_id', $customer->id);
        return Redirect::route('mounts.first');
    }
	/**
	 * Store a newly created resource in storage.
	 * POST /customers
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /customers/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
        $se = Input::get('se');
        $customer_id = Session::get('customer_id');
        if($customer_id != $id){
            exit;
        }
        $customer = Customer::find($id);
        $user_info = Session::get('user_info');
        $areas = Area::where('level',1)->get();
        if(!$se){
            return View::make('customers.edit_all',compact('customer','user_info','se'));
        }else{
            return View::make('customers.edit',compact('customer','user_info','se','areas'));
        }
	}

    public function ajax_edit(){
        $sex = Input::get('sex');
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        $customer->sex = $sex;
        $customer->save();
        echo 100;

    }
	/**
	 * Update the specified resource in storage.
	 * PUT /customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
        $customer = Customer::find($id);
        if($customer->supplement == 0){


            Customer::add_points($id,'supplement',1,0);//增加积分
            $customer->supplement = 1;

        }

        if(Input::get('save_type') == 'all'){
            $customer->avatar = Input::get('avatar');
            $customer->sex = Input::get('sex');
        }
        if(Input::get('name')){
            $customer->name = Input::get('name');
        }
        if(Input::get('mobile')){
            $customer->mobile = Input::get('mobile');
        }

        if(Input::get('p_id')){
            $customer->province = Input::get('p_id');
            $customer->city = Input::get('c_id');
        }


//        $customer->mobile = Input::get('mobile');
//        $customer->avatar = Input::get('avatar');
//        $customer->sex = Input::get('sex');
        $customer->save();
        return Redirect::route('customers.index');
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /customers/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

    /**
     * 用户中心
     */
    public function center(){
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        $user_info = Session::get('user_info');
        return View::make('customers.center',compact('customer','user_info'));
    }

    public function mounts(){

        $PATH = "http://aebo.qiniudn.com/";
        $customer_id = Session::get('customer_id');
        $mounts = Mount::where('customer_id',$customer_id)
            ->where('state','>',0)
            ->get();
        return View::make('customers.mounts',compact('mounts','PATH'));
    }
    public function services(){

        $customer_id = Session::get('customer_id');
        $services = Service::where('customer_id',$customer_id)->get();
        return View::make('customers.services',compact('services'));
    }
    public function orders(){
        $customer_id = Session::get('customer_id');
        $buyrecords = Buyrecord::where('customer_id',$customer_id)->get();
        return View::make('customers.orders',compact('buyrecords'));
    }

    public function filters(){
        $id = Input::get('id');
        $mount = Mount::find($id);
        $filterins = Filterin::where('mount_id',$mount->id)->get();
        $pics = Picture::where('mount_id',$id)->take(3)->get();
        return View::make('customers.filters',compact('filterins','filtercos','mount','pics'));
    }

    public function vip(){
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        if($customer->type == 5){//至尊vip显示
            $mountrecords = Mountrecord::where('invite_id',$customer_id)->orderBy('created_at', 'DESC')->get();
        }

        return View::make('customers.vip',compact('customer','mountrecords'));
    }
    public function normal(){
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        $pointrecords = Pointrecord::where('customer_id',$customer_id)->orderBy('created_at', 'DESC')->get();

        return View::make('customers.normal',compact('pointrecords','customer'));
    }


    public function pointrecords(){
        $customer_id = Session::get('customer_id');
        $customer = Customer::find($customer_id);
        return View::make('customers.pointrecords',compact('customer'));
    }

    public function ajax(){
        $id = Input::get('id');
        $areas = Area::where('father_id',$id)->get();

        return View::make('customers.ajax',compact('areas'));
    }


    function random($length)
    {
        $month = date('ym');
        $hash = $month;
        $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++)
        {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

    function login_error(){
        return View::make('errors.error');
    }

}